﻿using System;
using System.Collections.Generic;
using System.Linq;
using Brc_PaaS.Api.Common;
using Brc_PaaS.Api.Filters;
using Brc_PaaS.Common.Model.Redis;
using Brc_PaaS.Common.Model.System;
using Brc_PaaS.Common.Entity;
using Brc_PaaS.Common.Helper;
using Brc_PaaS.Common.Model.Basic;
using Brc_PaaS.Core.Redis;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Brc_PaaS.Bll.Redis;
using System.ComponentModel.DataAnnotations;

namespace Brc_PaaS.Api.PassController
{
    /// <summary>
    /// PaaS平台自身业务实现调用
    /// </summary>
    [Route("PaaS/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    [ApiExplorerSettings(GroupName = "PaaSBasic")]
    public class PaaSRedisController : AdminBaseController
    {
        private readonly object Locker = new object();
        private IRedis _redis;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="redis"></param>
        public PaaSRedisController(IConfiguration configuration, IRedis redis) : base(configuration)
        {
            _redis = redis;
        }

        /// <summary>
        /// 初始化数据库
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        
        //[LoginValidatorFilter]
        public ActionResult<BaseResponseModel> AddRedisDb(RedisInitRequestModel model)
        {
            model.CreateUserId = Guid.NewGuid().ToString("N");
            var result = _redis.InitRedisDb(model);
            return Ok(result);
        }
        /// <summary>
        /// PaaS自动为业务平台分配数据
        /// </summary>
        /// <returns></returns>
        
        [HttpPost]
        //[LoginValidatorFilter]
        public ActionResult<ResponseModel<Ass_Redis>> AutoDistributionDb([Required(ErrorMessage = "系统Id不能为空")][FromBody]string systemId)
        {
            ResponseModel<Ass_Redis> result = new ResponseModel<Ass_Redis>() { IsSuccess = false, Msg = string.Empty, Data = new Ass_Redis() };
            if (string.IsNullOrEmpty(systemId))
            {
                result.Msg = "未能获取到当前业务系统信息，请稍后再试";
                return Ok(result);
            }
            var model = _redis.AutoDistributionDb(systemId);
            return Ok(model);
        }

        /// <summary>
        /// 获取缓存数据库信息
        /// </summary>
        /// <returns></returns>
        
        [HttpGet]
        //[LoginValidatorFilter]
        public ActionResult<ResponseModel<Ass_Redis>> GetRedisDb([Required(ErrorMessage = "系统Id不能为空")]string systemId)
        {
            ResponseModel<Ass_Redis> result = new ResponseModel<Ass_Redis>() { IsSuccess = false, Msg = string.Empty, Data = new Ass_Redis() };
            if (string.IsNullOrEmpty(systemId))
            {
                result.Msg = "未能获取到当前业务系统信息，请稍后再试";
                return Ok(result);
            }
            using (var redisManager = new RedisManager(RedisServerEntity))
            {
                result.Data = redisManager.GetOrFetchFromHash<Ass_Redis>($"RedisDb:{systemId}", (id) => { return _redis.GetRedisDb(systemId).Data; });
            }
            if (string.IsNullOrEmpty(result.Data.Id))
            {
                result.Msg = "未能获取到对应数据库信息，请稍后再试";
                return Ok(result);
            }
            result.IsSuccess = true;
            return result;
        }
       
        
    }
}